// pages/my/publish/publish.js
import {
  requestUtil
} from "../../../utils/requestUtil"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId:0,
    myWorks:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let user = wx.getStorageSync('user')
    if(user!=null){
      this.setData({
        userId:user.userId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getWorksByUserId();

  },
  async getWorksByUserId(){
    const result = await requestUtil({
      url: '/recipe/getWorksByUserId/'+this.data.userId,
      method: 'GET'
    });
    console.log(result);
    this.setData({
      myWorks: result.data,
    })
  },
  async deleteRecipeById(id){
    const result = await requestUtil({
      url: '/recipe/'+id,
      method: 'DELETE'
    })
    this.getWorksByUserId();
  },
  toDetail(event){
    console.log(event.currentTarget.dataset);
    const recipeId=event.currentTarget.dataset.recipeid
    wx.navigateTo({
      url: '../../index/detail/detail?recipeId='+recipeId,
    })

  },
  // 删除我的作品
  deleteMywork(event){
    console.log(event.currentTarget.dataset);
    const recipeId=event.currentTarget.dataset.recipeid
    let that = this
    wx.showModal({
      title: '提示',
      content: '您确定要删除该作品吗',
      success(res) {
        if (res.confirm) {
          that.deleteRecipeById(recipeId)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  }

})